package com.igeek.compus.study.user.controller;

import com.igeek.compus.study.user.entity.User;
import com.igeek.compus.study.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("list.do")
    @ResponseBody
    public Map list(User user) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 业务代码，用户信息查询
            List<User> userList = userService.getUserList(user);

            result.put("data", userList);
            result.put("count", userList.size());

            result.put("code", 0);
            result.put("msg", "查询成功");

        }catch (Exception e) {
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "系统异常");
        }

        return result;
    }

    @RequestMapping("updateUser.do")
    @ResponseBody
    public int updateUser(User user){
        int i=0;
        i= userService.updateUser(user);
        return i;
    }

    @RequestMapping("getUserById.do")
    @ResponseBody
    public User getUserById(Integer id){
        return userService.getUserById(id);
    }

    @RequestMapping("toUserEdit.do")
    public ModelAndView toUserEdit(Integer id){
        Map<String ,Object> map=new HashMap<>();
        User user = getUserById(id);

        System.out.println(user);

        map.put("id",user.getId());
        map.put("nickName",user.getNickName());
        map.put("phone",user.getPhone());
        map.put("sex",user.getAge());
        map.put("email",user.getEmail());
        map.put("status",user.getStatus());

        return  new ModelAndView("/user/user_edit",map);
    }

}
